package com.mt.api.modules.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mt.api.core.page.PageFactory;
import com.mt.api.modules.domain.AppOrder;
import com.mt.api.modules.domain.bo.AppOrderListBo;
import com.mt.api.modules.domain.dto.*;
import com.mt.api.modules.manager.IOrderManager;
import com.mt.api.modules.mapper.AppOrderMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @Author: plover
 * @Date: 2022-01-02
 * @Description:
 */
@Component
public class OrderManagerImpl extends ServiceImpl<AppOrderMapper, AppOrder> implements IOrderManager {


    @Override
    public Page<AppOrder> getOrderPage(AppOrderListBo appOrderListBo) {
        Page page = PageFactory.defaultPage(appOrderListBo);
        List<AppOrder> list = baseMapper.getOrderPage(page, appOrderListBo);
        page.setRecords(list);
        return page;
    }

    @Override
    public List<OrderCustomerCountDto> getOrderCustomerCount(Integer lastDayNum) {
        return baseMapper.getOrderCustomerCount(lastDayNum);
    }

    @Override
    public List<OrderCargoCountDto> getOrderCargoCount(Integer lastDayNum) {
        return baseMapper.getOrderCargoCount(lastDayNum);
    }

    @Override
    public List<AppOrder> getOrderByDate(List<Integer> statusList, String date) {
        QueryWrapper<AppOrder> wrapper = Wrappers.query();
        wrapper.eq(StringUtils.isNotEmpty(date), "Date(finish_time)", date)
                .in("status", statusList);
        return baseMapper.selectList(wrapper);
    }

    @Override
    public AppOrder getLastOrderByCustomerId(Integer customerId) {
        LambdaQueryWrapper<AppOrder> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(AppOrder::getCustomerId, customerId)
                .orderByDesc(AppOrder::getOrderTime)
                .last("limit 1");
        return baseMapper.selectOne(wrapper);
    }

    @Override
    public int plusOutStoreQty(Integer id, Integer outStoreQty, Integer status, Date finishTime) {
        return baseMapper.plusOutStoreQty(id, outStoreQty, status, finishTime);
    }

    @Override
    public int subtractOutStoreQty(Integer id, Integer outStoreQty, Integer status) {
        return baseMapper.subtractOutStoreQty(id, outStoreQty, status);
    }

}
